package Model;
import java.util.Random;


/** 
 * @author Josh Weinflash
 * @Description: Stores data for the tiles, which makeup the Map
 */
public class Tile 
{
	private Player owner;
	private String type;
	private int foodProduction;
	private int energyProduction;
	private int oreProduction;
	private boolean muleHere;
	private Mule mule;
	
	/* private crystite? the production is listed as variable.
	   this is extra credit, so maybe work on it later  */
	
	/**
	 * Constructor for tile 
	 * @param String myType is the type of tile 
	 * (river, plain mountain1-3) to be intitialized
	 */
	public Tile(String myType)
	{
		if (myType == "R")
		{
			owner = null;
			type = myType;
			foodProduction = 4;
			energyProduction = 2;
			oreProduction = 0;
			muleHere = false;
		}
		
		else if (myType == "P")
		{
			owner = null;
			type = myType;
			foodProduction = 2;
			energyProduction = 3; 
			oreProduction = 1;
			muleHere = false;
		}
				
		else if (myType == "M1")
		{
			owner = null;
			type = myType;
			foodProduction = 1;
			energyProduction = 1; 
			oreProduction = 2;
			muleHere = false;
		}
		
		else if (myType == "M2")
		{
			owner = null;
			type = myType;
			foodProduction = 1;
			energyProduction = 1; 
			oreProduction = 3;
			muleHere = false;
		}
		
		else if (myType == "M3")
		{
			owner = null;
			type = myType;
			foodProduction = 1;
			energyProduction = 1; 
			oreProduction = 4;
			muleHere = false;
		}
		
		else if (myType == "T")
		{
			owner = null;
			type = myType;
			
		}
				
		else 
		{
			// do nothing, invalid type
			System.out.println(myType + "Is an invalid tile type");
		}
	}
	
	/**
	 * method that returns true the given tile is equivalent in 
	 * all possible ways to the inputed one.
	 * @param tile
	 * @return boolean
	 */
	public boolean equals(Tile tile) {
		boolean isEqual = false;
		if(this.type == tile.type && this.muleHere == tile.muleHere && this.owner == tile.owner){
			isEqual = true;
		}
		return isEqual;
	}
	
	/**
	 * getter for mule field
	 */
	public Mule getMule() {
		return mule;
	}
	
	/**
	 * setter for mule field
	 * @param m: mule parameter
	 */
	public void setMule(Mule m) {
		mule = m;
		muleHere = true;
	}

	
	/**
	 * production method for the food resource
	 */
	public void produceFood() {
		String muleType = mule.getType();
		
		if (muleType == "Food" && owner.getEnergy() > 0) {
			
			if (type == "R") {
				owner.incrementFood(4);
				
			}
			
			else if (type == "P") {
				owner.incrementFood(2);
			}
			
			else {
				owner.incrementFood(1);
			}
			
			owner.decrementEnergy();  // subtract 1 unit of energy for production
			
		}
		
	}
	
	/**
	 * production method for the energy resource
	 */
	public void produceEnergy() {
		String muleType = mule.getType();
		
		if (muleType == "Energy" && owner.getEnergy() > 0) {
			
			if (type == "R") {
				owner.incrementEnergy(2);
			}
			
			else if (type == "P") {
				owner.incrementEnergy(3);
			}
			
			else {
				owner.incrementEnergy(1);
			}
			
			owner.decrementEnergy(); //subtract 1 unit of energy for production
					
		}
	}
	
	
	/**
	 * production method for the ore resource
	 */
	public void produceOre() {
		String muleType = mule.getType();
		
		if (muleType == "Smithore" && owner.getEnergy() > 0) {
			
			if (type == "P") {
				owner.incrementOre(1);
			}
			
			else if (type == "M1") {
				owner.incrementOre(2);
			}
			
			else if (type == "M2") {
				owner.incrementOre(3);
			}
			
			else {
				owner.incrementOre(4);
			}
			
			owner.decrementEnergy(); //subtract 1 unit of energy for production
		}
	}
	
	/**
	 * helper method for production
	 */
	public void produce() {
		
		if (owner != null && muleHere == true) {
			
			produceFood();
			produceEnergy();
			produceOre();
			
			System.out.println(owner.getName() + " Food: " + owner.getFood());
			System.out.println(owner.getName() + " Energy: " + owner.getEnergy());
			System.out.println(owner.getName() + " Ore: " + owner.getOre());
		}
	}
		

	/**
	 * Getter for owner field
	 */
	public Player getOwner() {
		return owner;
	}
	
	/** 
	 * Setter for owner field
	 */
	public void setOwner(Player newOwner) {
		owner = newOwner;
	}
	
	/**
	 * Getter for type field
	 */
	public String getType() {
		return type;
	}
	
	/** 
	 * Getter for foodProduction field
	 */
	public int getFoodProduction() {
		return foodProduction;
	}
	
	/**
	 * Getter for energyProduction field
	 */
	public int getEnergyProduction() {
		return energyProduction;
	}
	
	/**
	 * Getter for oreProduction field
	 */
	public int getOreProduction() {
		return oreProduction;
	}
	
	/**
	 * see if mule a mule is emplaced on this tile
	 * (i.e. Getter for muleHere)
	 */
	public boolean getMuleHere() {
		return muleHere;
	}
	
	/**
	 * player emplaces/demplaces a mule on this tile
	 * (i.e. setter for muleHere field)
	 */
	public void setMuleHere(boolean bool) {
		muleHere = bool;
	}
	
	/** 
	 * buyTile uses a random number to calculate the tile cost and subtracts this
	 * from the purchasing player's funds, IF the player has enough money.
	 * @param player: the player purchasing the tile
	 * @param roundNum: the current round number
	 */
	public void buyTile(Player player, int roundNum) {
		Random r = new Random();
		int price = 300 + roundNum * r.nextInt(101);
		
		if (roundNum <= 2) {
			owner = player;	
			}
		
		else if (player.getMoney() >= price) {
			owner = player;
			owner.addMoney(-1*price);
		}
		
	}
	
}
